/*
 * Copyright (C) 2021 HiHope Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *
 * limitations under the License.
 */

#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "hi_io.h"
#include "iot_pwm.h"
#include "iot_errno.h"
#include "hi_adc.h"
#include "hi_gpio.h"

#define RED_LED_PIN_NAME        10
#define GREEN_LED_PIN_NAME      11
#define BLUE_LED_PIN_NAME       12
#define HUMAN_SENSOR_CHAN_NAME  3
#define LIGHT_SENSOR_CHAN_NAME  4

#define LED_PWM_FUNCTION        5
#define IOT_PWM_PORT_PWM1       1
#define IOT_PWM_PORT_PWM2       2
#define IOT_PWM_PORT_PWM3       3
#define IOT_PWM_PORT_PWM_MAX    4
#define IOT_PWM_DUTY_MAX        100

#define LED_BRIGHT              IOT_GPIO_VALUE1
#define LED_DARK                IOT_GPIO_VALUE0

#define NUM_BLINKS              2
#define NUM_SENSORS             2
#define NUM_2                   2
#define NUM_3                   3

#define ADC_RESOLUTION          4096
#define PWM_FREQ_DIVITION       64000
#define CLK_160M                160000000

#define STACK_SIZE             (4096)
#define DELAY_300MS            (300 * 1000)
#define DELAY_10MS             (10 * 1000)

#define DELAY_US     (100000)
hi_u16 value;

static const unsigned int pins[] = {RED_LED_PIN_NAME, GREEN_LED_PIN_NAME, BLUE_LED_PIN_NAME};


static hi_u32 getADC(hi_adc_channel_index idx){


    return hi_adc_read(idx, &value,HI_ADC_EQU_MODEL_4, HI_ADC_CUR_BAIS_DEFAULT, 0);

}

static void CorlorfulLightTask2(void)
{
	
     

	while (1) {
        
       getADC(HI_ADC_CHANNEL_4);//电平无法做多范围
       printf("%u\n",(unsigned int )value);
       usleep(DELAY_US);
        switch ((unsigned int)value>=1600)
        {
            case 1:
                getADC(HI_ADC_CHANNEL_3);
                if((unsigned int)value>=1600){
                    printf("%u\n",(unsigned int)value);
                    usleep(10000);//减少判断时间
                        for (int i = 1; i < IOT_PWM_PORT_PWM_MAX; i++) {
                            // use PWM control BLUE LED brightness
                            for (int j = 1; j <= IOT_PWM_DUTY_MAX; j *= NUM_2) {
                                IoTPwmStart(i, j, CLK_160M / PWM_FREQ_DIVITION);
                                usleep(DELAY_300MS);
                                IoTPwmStop(i);
                            }
                        }
                    
                }
                
                break;
            
            default:
                break;
        }
    }

}


static void initALL(void){
	
	//gpio
	IoTGpioInit(RED_LED_PIN_NAME);
    IoTGpioInit(GREEN_LED_PIN_NAME);
    IoTGpioInit(BLUE_LED_PIN_NAME);

    // set Red/Green/Blue LED pin as output
    IoTGpioSetDir(RED_LED_PIN_NAME, IOT_GPIO_DIR_OUT);
    IoTGpioSetDir(GREEN_LED_PIN_NAME, IOT_GPIO_DIR_OUT);
    IoTGpioSetDir(BLUE_LED_PIN_NAME, IOT_GPIO_DIR_OUT);
	
	//pwm复用 
	// set Red/Green/Blue LED pin to pwm function
    hi_io_set_func(RED_LED_PIN_NAME, LED_PWM_FUNCTION);
    hi_io_set_func(GREEN_LED_PIN_NAME, LED_PWM_FUNCTION);
    hi_io_set_func(BLUE_LED_PIN_NAME, LED_PWM_FUNCTION);

    IoTPwmInit(IOT_PWM_PORT_PWM1); // R
    IoTPwmInit(IOT_PWM_PORT_PWM2); // G
    IoTPwmInit(IOT_PWM_PORT_PWM3); // B
    
    //adc 
    hi_gpio_init();
    hi_io_set_func(HI_GPIO_IDX_9, 0);
    hi_gpio_set_dir(HI_GPIO_IDX_9, HI_GPIO_DIR_IN);
    
    hi_gpio_init();
    hi_io_set_func(HI_GPIO_IDX_7, 0);
    hi_gpio_set_dir(HI_GPIO_IDX_7, HI_GPIO_DIR_IN);
    
    
	
} 




static void ColorfulLightDemo(void)
{
    osThreadAttr_t attr;

    initALL();

    attr.name = "CorlorfulLightTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = STACK_SIZE;
    attr.priority = osPriorityNormal;

    if (osThreadNew(CorlorfulLightTask2, NULL, &attr) == NULL) {
        printf("[ColorfulLightDemo] Falied to create CorlorfulLightTask!\n");
    }
}

APP_FEATURE_INIT(ColorfulLightDemo);